package com.enlighten.featureExtractor.coinFeatureExtractor;

import java.awt.image.BufferedImage;

import com.enlighten.featureExtractor.ICoinFeatureExtractor;
import com.enlighten.featureExtractor.coin.RadiusBasedCoin;
import com.enlighten.featureExtractor.coinFeature.RadiusBasedCoinFeature;
import com.enlighten.featureExtractor.exceptions.IncorrectImageSizeException;
import com.enlighten.featureExtractor.util.ImageUtils;

public class RadiusBasedCoinFeatureExtractor implements ICoinFeatureExtractor<RadiusBasedCoinFeature,RadiusBasedCoin> {

	@Override
	public RadiusBasedCoin extractFrom(BufferedImage image) {
		int width = image.getWidth();
		int height = image.getHeight();
		
		RadiusBasedCoin radiusBasedCoin = null;
		try {
			radiusBasedCoin = new RadiusBasedCoin(width, height);
		} catch (IncorrectImageSizeException e) {
			image = ImageUtils.getCompatibleImage(image);
			try {
				radiusBasedCoin = new RadiusBasedCoin(image.getWidth(),image.getHeight());
			} catch (IncorrectImageSizeException e1) {
				throw new RuntimeException(e);
			}
		}
		
		for(int i=0; i < height; i++){
			for(int j=0; j < width; j++){
				radiusBasedCoin.addData(j,i,image.getRGB(j, i));
			}
		}
		return radiusBasedCoin;
	}

}
